<?php

namespace app\bbs\controller;

use think\Controller;
use think\Db;
use app\bbs\model\task\bbs_task_list;
use think\Session;
use app\bbs\model\task\bbs_task_speed_of_progress_list;

class Task extends Controller
{
    public function index()
    {
        $bbs_task_list = new bbs_task_list;
        $head = head();
        $time = time();
        $mission_unanswere_id_order = array();
        $user_info  = Session::get('bbs_user_info');
        if ($user_info) {
            $bbs_jurisdiction_level_id = Db::table('bbs_jurisdiction_level')
                ->where(['jurisdiction_number' => $user_info['user_jurisdiction']])
                ->field('id')
                ->find()['id'];
            $where['bbs_task_list.authority'] = array('like', "%,$bbs_jurisdiction_level_id,%");

            $mission_unanswere  = $bbs_task_list->Field(['type', 'datetimepicker_end', 'datetimepicker_start', 'article_id', 'extended_task_links', 'task_name', 'mission_statement', 'id'])
                ->where($where)
                ->select()->toArray(); #所有任务
            $ongoing_tasks = array(); #进行中的任务
            $completed_tasks = array(); #已完成的项目
            $mission_failure = array(); #任务失败
            $task_exception = array(); #任务异常
            $bbs_task_speed_of_progress_list = new bbs_task_speed_of_progress_list;
            $database_bbs_task_speed_of_progress_list = $bbs_task_speed_of_progress_list::all(['user_id' => $user_info['id']]);
            if ($database_bbs_task_speed_of_progress_list) {
                $database_bbs_task_speed_of_progress_list = $database_bbs_task_speed_of_progress_list->toArray();
                foreach ($mission_unanswere as $key => $value) {
                    $mission_unanswere_id_order[$value['id']] = $value; //把所有获取到的任务 的id 拿出来当作主键
                }
                foreach ($database_bbs_task_speed_of_progress_list as $key => $value) {
                    switch ($value['start']) {
                        case '1':
                            if (isset($mission_unanswere_id_order[$value['task_id']])) { // 判断这个变量是否存在
                                $mission_unanswere_id_order[$value['task_id']]['start'] = 1;
                                array_push($ongoing_tasks, $mission_unanswere_id_order[$value['task_id']]);
                                unset($mission_unanswere_id_order[$value['task_id']]);
                            }
                            break;
                        case '2':
                            if (isset($mission_unanswere_id_order[$value['task_id']])) { // 判断这个变量是否存在
                                $mission_unanswere_id_order[$value['task_id']]['id'] = $value['id'];
                                $mission_unanswere_id_order[$value['task_id']]['start'] = 2;
                                array_push($ongoing_tasks, $mission_unanswere_id_order[$value['task_id']]);
                                unset($mission_unanswere_id_order[$value['task_id']]);
                            }
                            break;
                        case '3':
                            if (isset($mission_unanswere_id_order[$value['task_id']])) { // 判断这个变量是否存在
                                array_push($completed_tasks, $mission_unanswere_id_order[$value['task_id']]);
                                unset($mission_unanswere_id_order[$value['task_id']]);
                            }
                            break;
                        case '4':
                            if (isset($mission_unanswere_id_order[$value['task_id']])) { // 判断这个变量是否存在
                                array_push($mission_failure, $mission_unanswere_id_order[$value['task_id']]);
                                unset($mission_unanswere_id_order[$value['task_id']]);
                            }
                            break;
                        default:
                            if (isset($mission_unanswere_id_order[$value['task_id']])) { // 判断这个变量是否存在
                                array_push($task_exception, $mission_unanswere_id_order[$value['task_id']]);
                                unset($mission_unanswere_id_order[$value['task_id']]);
                            }
                            break;
                    }
                }

                foreach ($mission_unanswere_id_order as $key => $value) { //过滤时间未开始 或已结束的
                    if ($value['datetimepicker_start'] > $time || $value['datetimepicker_end'] < $time) {
                        unset($mission_unanswere_id_order[$key]);
                    }
                }
            } else { }
            return $this->fetch('task_index', ['head' => $head, 'mission_unanswere' => $mission_unanswere_id_order, 'ongoing_tasks' => $ongoing_tasks, 'completed_tasks' => $completed_tasks, 'mission_failure' => $mission_failure, 'task_exception' => $task_exception,]);
        } else {
            return $this->fetch('error');
        }
    }
    public function task_index_receiving_tasks()
    {
        $bbs_task_list = new bbs_task_list;
        $user_info  = Session::get('bbs_user_info');
        $article_id  = input('post.id') * 1;
        if ($user_info && $article_id) {
            $bbs_jurisdiction_level_id = Db::table('bbs_jurisdiction_level')
                ->where(['jurisdiction_number' => $user_info['user_jurisdiction']])
                ->field('id')
                ->find()['id'];
            $time = time();
            $where['datetimepicker_start'] = array('<', "$time");
            $where['datetimepicker_end'] = array('>', "$time");
            $where['authority'] = array('like', "%,$bbs_jurisdiction_level_id,%");
            $task = $bbs_task_list->where($where)->where('id', 'eq', $article_id)->find();
            if ($task) {
                $bbs_task_speed_of_progress_list = new bbs_task_speed_of_progress_list;
                $bbs_task_speed_of_progress_list->task_id = $task['id'];
                $bbs_task_speed_of_progress_list->user_id = $user_info['id'];
                $bbs_task_speed_of_progress_list->start = 1;
                $bbs_task_speed_of_progress_list->add_time = date('Y-m-d H:i:s');
                $bbs_task_speed_of_progress_list->save();
                $Data['code'] = '0';
                $Data['ErrorPosition'] = '';
                $Data['ErrorMsg'] = 'success ';
                return json($Data);
            } else {
                $Data['code'] = '10001';
                $Data['ErrorPosition'] = 'Task';
                $Data['ErrorMsg'] = 'taskError ';
                return json($Data);
            }
        } else {
            $Data['code'] = '10000';
            $Data['ErrorPosition'] = 'Task';
            $Data['ErrorMsg'] = 'UserInfoLoseOrArticleIdLose ';
            return json($Data);
        }
    }
    public function task_index_reward()
    {
        $user_info  = Session::get('bbs_user_info');
        $id  = input('post.id') * 1;
        if ($user_info && $id) {
            $bbs_jurisdiction_level_id = Db::table('bbs_jurisdiction_level')
                ->where(['jurisdiction_number' => $user_info['user_jurisdiction']])
                ->field('id')
                ->find()['id'];
            $time = time();
            $where['bbs_task_list.datetimepicker_start'] = array('<', "$time");
            $where['bbs_task_list.datetimepicker_end'] = array('>', "$time");
            $where['bbs_task_list.authority'] = array('like', "%,$bbs_jurisdiction_level_id,%");

            $bbs_task_list = Db::table('bbs_task_speed_of_progress_list')
                ->where([
                    'bbs_task_speed_of_progress_list.id' => $id,
                    'bbs_task_speed_of_progress_list.user_id' => $user_info['id'],
                    'bbs_task_speed_of_progress_list.start' => 2,
                ])
                ->alias('bbs_task_speed_of_progress_list')
                ->join('bbs_task_list', "bbs_task_list.id = bbs_task_speed_of_progress_list.task_id")
                ->join('bbs_task_award_name', "bbs_task_list.bbs_user_integral = bbs_task_award_name.id") // 奖励的名称
                ->where($where)
                ->field('bbs_task_award_name.data_base_name,bbs_task_list.number,bbs_task_list.task_name')
                ->find();

            if ($bbs_task_list) {

                $bbs_user_integral_list = Db::table('bbs_user_integral_list')
                    ->where(['user_id' => $user_info['id']])->order('id desc')->find();

                $integral_count = 0; //总积分
                $download_currency_count = 0; //下载积分
                if ($bbs_task_list['data_base_name'] == 'download_currency') {
                    $download_currency_count = $bbs_task_list['number']; //下载积分
                } else if ($bbs_task_list['data_base_name'] == 'integral') {
                    $integral_count = $bbs_task_list['number']; //总积分
                } else {
                    $bbs_task_list = Db::table('bbs_task_speed_of_progress_list')
                        ->where([
                            'bbs_task_speed_of_progress_list.id' => $id,
                            'bbs_task_speed_of_progress_list.user_id' => $user_info['id'],
                            'bbs_task_speed_of_progress_list.start' => 2,
                        ])
                        ->update(['start' => 5]);
                    $Data['code'] = '10001';
                    $Data['ErrorPosition'] = 'task_index_reward';
                    $Data['ErrorMsg'] = 'error ' . __LINE__;
                    return json($Data);
                }
                Db::table('bbs_task_speed_of_progress_list')
                    ->where([
                        'bbs_task_speed_of_progress_list.id' => $id,
                        'bbs_task_speed_of_progress_list.user_id' => $user_info['id'],
                        'bbs_task_speed_of_progress_list.start' => 2,
                    ])
                    ->update(['start' => 3]);

                Db::table('bbs_user_integral_list')->insert([
                    'user_id' => $user_info['id'],
                    $bbs_task_list['data_base_name'] => $bbs_task_list['number'],
                    'info' => "完成任务 $bbs_task_list[task_name] 的奖励",
                    'reward_or_punishment' => 0,
                    'integral_count' => $bbs_user_integral_list['integral_count'] + $integral_count,
                    'download_currency_count' => $bbs_user_integral_list['download_currency_count'] + $download_currency_count,
                ]);

                $bbs_user_integral = Db::table('bbs_user_integral')
                    ->where(['user_id' => $user_info['id']])
                    ->find();
                Db::table('bbs_user_integral')
                    ->where(['user_id' => $user_info['id']])
                    ->update([$bbs_task_list['data_base_name'] => $bbs_user_integral[$bbs_task_list['data_base_name']] + $bbs_task_list['number']]);
                $Data['code'] = '0';
                $Data['ErrorPosition'] = '';
                $Data['ErrorMsg'] = 'success';
                return json($Data);
            } else {
                $bbs_task_list = Db::table('bbs_task_speed_of_progress_list')
                    ->where([
                        'bbs_task_speed_of_progress_list.id' => $id,
                        'bbs_task_speed_of_progress_list.user_id' => $user_info['id'],
                        'bbs_task_speed_of_progress_list.start' => 2,
                    ])
                    ->update(['start' => 5]);
                $Data['code'] = '10001';
                $Data['ErrorPosition'] = 'task_index_reward';
                $Data['ErrorMsg'] = 'error ' . __LINE__;
                return json($Data);
            }
        } else {
            $Data['code'] = '10000';
            $Data['ErrorPosition'] = 'task_index_reward';
            $Data['ErrorMsg'] = 'UserInfoLoseOrArticleIdLose ';
            return json($Data);
        }
    }
    public function task_index_info()
    {
        $user_info  = Session::get('bbs_user_info');
        $id  = input('post.id') * 1;
        if ($user_info && $id) {
            $bbs_task_list = Db::table('bbs_task_list')
                ->alias('bbs_task_list')
                ->where(['bbs_task_list.id' => $id])
                ->join('bbs_task_award_name', 'bbs_task_award_name.id = bbs_task_list.bbs_user_integral')
                ->field([
                    'bbs_task_list.type',
                    'bbs_task_list.extended_task_links',
                    'bbs_task_list.task_name',
                    'bbs_task_list.mission_statement',
                    'bbs_task_award_name.award_name',
                    'bbs_task_list.article_id as id',
                    'bbs_task_list.number',
                    'bbs_task_list.authority',
                ])
                ->find();
            if ($bbs_task_list) {
                $PermissionGroups = array();
                foreach (preg_grep("/\S+/i", explode(',', $bbs_task_list['authority'])) as $key => $value) {
                    $temp = Db::table('bbs_jurisdiction_level')
                        ->where(['id' => $value])
                        ->field('level_name,id')
                        ->find();
                    array_push($PermissionGroups, [$temp]);
                }
                $bbs_task_list['livel_name_list'] = $PermissionGroups;
                $bbs_task_list['task_number'] = Db::table('bbs_task_speed_of_progress_list')->where(['task_id' => $id])->count(); //参加活动人数
                $bbs_task_list['task_number_info'] = Db::table('bbs_task_speed_of_progress_list')
                    ->alias('bbs_task_speed_of_progress_list')
                    ->where(['bbs_task_speed_of_progress_list.task_id' => $id])
                    ->order('bbs_task_speed_of_progress_list.id desc')
                    ->join('user', 'bbs_task_speed_of_progress_list.user_id = user.id')
                    ->field('user.nickname,user.head_src,user.user_name,user.id')
                    ->limit(6)->select(); //参加活动的头像名称
                $this->success(null, null, $bbs_task_list);
            } else {
                $bbs_task_list = Db::table('bbs_task_speed_of_progress_list')
                    ->where([
                        'bbs_task_speed_of_progress_list.id' => $id,
                        'bbs_task_speed_of_progress_list.user_id' => $user_info['id'],
                        'bbs_task_speed_of_progress_list.start' => 2,
                    ])
                    ->update(['start' => 5]);
            }
        } else {
            $Data['code'] = '10000';
            $Data['ErrorPosition'] = 'task_index_reward';
            $Data['ErrorMsg'] = 'UserInfoLoseOrArticleIdLose ';
            return json($Data);
        }
    }
    public function task_index_integral_query()
    {
        $id = 1;
        $bbs_user_integral_list['list'] = Db::table('bbs_user_integral_list')->where(['user_id' => $id])->select();
        $this->success(null, null, $bbs_user_integral_list);
    }
}
